<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AdminRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if (request()->method() == 'POST') {
            return [
                'name' => 'required|between:2,10|unique:admins',
                'password' => 'required',
                'avatr' => 'max:128',
                'game_id' => 'required',
                'channel_table_id' => 'required',
                'status' => 'required:integer',
            ];
        } else {
            $where = [
                'name' => request()->get('name'),
            ];

            return [
                'name' => 'required|between:2,10|unique:admins',
                'name' => [
                    'required',
                    'between:2,10',
                    Rule::unique('admins')
                        ->where(function ($query) use ($where) {
                            return $query->where($where)->where('id', '!=', request()->route('admin'));
                        })
                ],
                'avatr' => 'max:128',
                'game_id' => 'required',
                'channel_table_id' => 'required',
                'status' => 'required:integer',
            ];
        }
    }

    /**
     * 提示信息s
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '用户名不能为空',
            'name.between' => '用户名长度应该在2~10位之间',
            'name.unique' => '用户名不能重复',
            'password.required' => '密码不能为空',
            'avatr.max' => '头像不能超过128个字符',
            'game_id.required' => '游戏不能为空',
            'channel_table_id.required' => '渠道不能为空',
            'status.required' => '状态不能为空',
            'status.integer' => '表单不合法',
        ];
    }
}
